
<!DOCTYPE html>
<!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]-->
<!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]-->
<!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js"><!--<![endif]-->
<head>
  <meta charset="utf-8">
  <title>
    
  一篇文章搞定 centos7 上的 mysql 安装 - LZH-DEV-TECH-SHARE
  

  </title>
  <meta name="author" content="">
  <meta name="description" content="">

  <meta name="HandheldFriendly" content="True">
  <meta name="MobileOptimized" content="320">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  
  <link href="asset/css/screen.css" media="screen, projection" rel="stylesheet" type="text/css">
  <link href="atom.xml" rel="alternate" title="LZH-DEV-TECH-SHARE" type="application/atom+xml">
  <script src="asset/js/modernizr-2.0.js"></script>
  <script src="asset/js/jquery.min.js"></script>
  <script src="asset/highlightjs/highlight.pack.js"></script>
  <link href="asset/highlightjs/styles/solarized_light.css" media="screen, projection" rel="stylesheet" type="text/css">
  <script>hljs.initHighlightingOnLoad();</script>

  <style type="text/css">
  .cat-children-p{ padding: 6px 0px;}
  .hljs{background: none;}
  </style>
  <script type="text/javascript">
  var isAddSildbar = true;
  </script>
  <script src="asset/js/octopress.js" type="text/javascript"></script>
</head>
<script type="text/javascript">
//链接新开窗口
function addBlankTargetForLinks () {
  $('a[href^="http"]').each(function(){
      $(this).attr('target', '_blank');
  });
}
$(document).ready(function(event) {
  addBlankTargetForLinks();
});
</script>
<body   >
  <header role="banner"><hgroup>
  <h1><a href="index.html">LZH-DEV-TECH-SHARE</a></h1>
  
    <h2></h2>
  
</hgroup>

</header>
  <nav role="navigation"><ul class="subscription" data-subscription="rss">
  <li><a href="atom.xml" rel="subscribe-rss" title="subscribe via RSS">RSS</a></li>
  
</ul>
  
<form action="http://google.com/search" method="get">
  <fieldset role="search">
    <input type="hidden" name="q" value="site:" />
    <input class="search" type="text" name="q" results="0" placeholder="Search"/>
  </fieldset>
</form>
  
<ul class="main-navigation">

  <li id=""><a target="self" href="index.html">Home</a></li>

  <li id=""><a target="_self" href="archives.html">Archives</a></li>

</ul>

</nav>
  <div id="main">
    <div id="content"> 
<div>
	<article class="hentry" role="article">
	<header>
			  	<h1 class="entry-title">一篇文章搞定 centos7 上的 mysql 安装</h1>
				<p class="meta"><time datetime="2018-10-30T14:32:24+08:00" pubdate data-updated="true">2018/10/30</time></p>
			 </header>
		  	<div class="entry-content">
			  	<ul>
<li>
<a href="#toc_0">下载并安装MySQL官方的 Yum Repository</a>
</li>
<li>
<a href="#toc_1">开始安装MySQL服务器。</a>
</li>
<li>
<a href="#toc_2">MySQL数据库设置</a>
<ul>
<li>
<a href="#toc_3">首先启动MySQL</a>
</li>
<li>
<a href="#toc_4">查看MySQL运行状态，运行状态如图：</a>
</li>
<li>
<a href="#toc_5">进入数据库：</a>
</li>
<li>
<a href="#toc_6">修改初始密码</a>
</li>
<li>
<a href="#toc_7">避免mysql自动更新</a>
</li>
<li>
<a href="#toc_8">mysql root 用户远程访问支持</a>
<ul>
<li>
<a href="#toc_9">登陆mysql</a>
</li>
<li>
<a href="#toc_10">查看 user 表数据</a>
</li>
<li>
<a href="#toc_11">实现远程连接(改表法)</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>


<blockquote>
<p>在CentOS中默认安装有MariaDB，这个是MySQL的分支，但为了需要，还是要在系统中安装MySQL，而且安装完成之后可以直接覆盖掉MariaDB。</p>
</blockquote>

<h2 id="toc_0">下载并安装MySQL官方的 Yum Repository</h2>

<pre><code class="language-shell">[root@localhost ~]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
</code></pre>

<p>使用上面的命令就直接下载了安装用的Yum Repository，大概25KB的样子，然后就可以直接yum安装了。</p>

<pre><code class="language-shell">[root@localhost ~]# yum -y install mysql57-community-release-el7-10.noarch.rpm
</code></pre>

<h2 id="toc_1">开始安装MySQL服务器。</h2>

<pre><code class="language-shell">[root@localhost ~]# yum -y install mysql-community-server
</code></pre>

<p>这步可能会花些时间，安装完成后就会覆盖掉之前的mariadb。<br/>
<img src="media/15408811446857/15408813515173.jpg" alt=""/></p>

<p>至此MySQL就安装完成了，然后是对MySQL的一些设置。</p>

<h2 id="toc_2">MySQL数据库设置</h2>

<h3 id="toc_3">首先启动MySQL</h3>

<pre><code class="language-shell">[root@localhost ~]# systemctl start  mysqld.service
</code></pre>

<h3 id="toc_4">查看MySQL运行状态，运行状态如图：</h3>

<pre><code class="language-shell">[root@localhost ~]# systemctl status mysqld.service
</code></pre>

<p><img src="media/15408811446857/15408813962176.jpg" alt=""/></p>

<p>此时MySQL已经开始正常运行，不过要想进入MySQL还得先找出此时root用户的密码，通过如下命令可以在日志文件中找出密码：</p>

<p>[root@localhost ~]# grep &quot;password&quot; /var/log/mysqld.log<br/>
<img src="media/15408811446857/15408814188529.jpg" alt=""/></p>

<h3 id="toc_5">进入数据库：</h3>

<pre><code class="language-shell">[root@localhost ~]# mysql -uroot -p
</code></pre>

<p>输入初始密码，此时不能做任何事情，因为MySQL默认必须修改密码之后才能操作数据库：</p>

<h3 id="toc_6">修改初始密码</h3>

<pre><code class="language-shell">mysql&gt; ALTER USER &#39;root&#39;@&#39;localhost&#39; IDENTIFIED BY &#39;new password&#39;;
</code></pre>

<p>这里有个问题，新密码设置的时候如果设置的过于简单会报错：<br/>
<img src="media/15408811446857/15408814703369.jpg" alt=""/></p>

<p>原因是因为MySQL有密码设置的规范，具体是与validate_password_policy的值有关：<br/>
 <img src="media/15408811446857/15408815243747.jpg" alt=""/></p>

<p>MySQL完整的初始密码规则可以通过如下命令查看：</p>

<pre><code class="language-mysql">mysql&gt; SHOW VARIABLES LIKE &#39;validate_password%&#39;;
+--------------------------------------+-------+
| Variable_name                        | Value |
+--------------------------------------+-------+
| validate_password_check_user_name    | OFF   |
| validate_password_dictionary_file    |       |
| validate_password_length             | 4     |
| validate_password_mixed_case_count   | 1     |
| validate_password_number_count       | 1     |
| validate_password_policy             | LOW   |
| validate_password_special_char_count | 1     |
+--------------------------------------+-------+
7 rows in set (0.01 sec)
</code></pre>

<p>密码的长度是由validate_password_length决定的，而validate_password_length的计算公式是：</p>

<pre><code class="language-shell">validate_password_length = validate_password_number_count + validate_password_special_char_count + (2 * validate_password_mixed_case_count)
</code></pre>

<p>我的是已经修改过的，初始情况下第一个的值是ON，validate_password_length是8。可以通过如下命令修改：</p>

<pre><code class="language-shell">mysql&gt; set global validate_password_policy=0;
mysql&gt; set global validate_password_length=1;
</code></pre>

<p>设置之后就是我上面查出来的那几个值了，此时密码就可以设置的很简单，例如1234之类的。到此数据库的密码设置就完成了。</p>

<h3 id="toc_7">避免mysql自动更新</h3>

<p>但此时还有一个问题，就是因为安装了Yum Repository，以后每次yum操作都会自动更新，需要把这个卸载掉：</p>

<pre><code class="language-shell">[root@localhost ~]# yum -y remove mysql57-community-release-el7-10.noarch
</code></pre>

<p>此时才算真的完成了。</p>

<h3 id="toc_8">mysql root 用户远程访问支持</h3>

<h4 id="toc_9">登陆mysql</h4>

<pre><code class="language-shell">mysql -u root -p
</code></pre>

<h4 id="toc_10">查看 user 表数据</h4>

<pre><code class="language-shell">mysql&gt; use mysql;
Database changed
mysql&gt; select host,user from user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| localhost | mysql.session |
| localhost | mysql.sys     |
| localhost | root          |
+-----------+---------------+
3 rows in set (0.00 sec)
</code></pre>

<h4 id="toc_11">实现远程连接(改表法)</h4>

<pre><code class="language-shell">update user set host = &#39;%&#39; where user = &#39;root&#39;;
</code></pre>

<p>这样在远端就可以通过root用户访问Mysql.</p>

			</div>

		
	  
		<footer>
		 <p class="meta">

			<strong>Categories:</strong>&nbsp; 
			<span class="categories">
			
			    <a class='category' href='other.html'>other</a>&nbsp;
			 
			</span>
		    </p>
		    <p class="meta">
		      
		 </p>
	    
		<div class="sharing">
		  
          

          

		</div>

	    <p class="meta">
	    
	        <a class="basic-alignment left" href="15410718240579.html" 
	        title="Previous Post: 如何使用PM2进程管理器, 守护java进程">&laquo; 如何使用PM2进程管理器, 守护java进程</a>
	    
	    
	        <a class="basic-alignment right" href="15408703788547.html" 
	        title="Next Post: 一篇文章搞定 centos7 下安装 nvm和node">一篇文章搞定 centos7 下安装 nvm和node &raquo;</a>
	    
	    </p>
	  </footer>
	</article>
</div>
 <aside class="sidebar"> 

	<section>
	  <h1>Categories</h1>
	  <ul id="recent_posts">
	  
	      <li class="post">
	        <a href="version-control.html"><strong>version-control&nbsp;(2)</strong></a>
	        
	        
	        
	      </li>
	  
	      <li class="post">
	        <a href="shell.html"><strong>shell&nbsp;(1)</strong></a>
	        
	        
	        
	      </li>
	  
	      <li class="post">
	        <a href="back-end.html"><strong>back-end&nbsp;(4)</strong></a>
	        
	        
	        
	      </li>
	  
	      <li class="post">
	        <a href="front-end.html"><strong>front-end&nbsp;(5)</strong></a>
	        
	        
	        
	      </li>
	  
	      <li class="post">
	        <a href="version-control-1.html"><strong>version-control&nbsp;(1)</strong></a>
	        
	        
	        
	      </li>
	  
	      <li class="post">
	        <a href="other.html"><strong>other&nbsp;(2)</strong></a>
	        
	        
	        
	      </li>
	   
	  </ul>
	</section>
	<section>
	  <h1>Recent Posts</h1>
	  <ul id="recent_posts">
	  
	      
		      <li class="post">
		        <a href="15413898294420.html">RESTful API 最佳实践</a>
		      </li>
	     
	  
	      
		      <li class="post">
		        <a href="15411425055347.html">LZH-DEV-GROUP Git Workflow 实践详解</a>
		      </li>
	     
	  
	      
		      <li class="post">
		        <a href="15410718240579.html">如何使用PM2进程管理器, 守护java进程</a>
		      </li>
	     
	  
	      
		      <li class="post">
		        <a href="15408811446857.html">一篇文章搞定 centos7 上的 mysql 安装</a>
		      </li>
	     
	  
	      
		      <li class="post">
		        <a href="15408703788547.html">一篇文章搞定 centos7 下安装 nvm和node</a>
		      </li>
	     
	  
	      
	  
	      
	  
	      
	  
	      
	  
	      
	  
	      
	  
	      
	  
	      
	  
	      
	  
	      
	   
	  </ul>
	</section>
	
</aside> </div></div>
  <footer role="contentinfo"><p>
  Copyright &copy; 2014 -  -
  <span class="credit">Powered by <a target="_blank" href="http://www.mweb.im">MWeb</a> &nbsp;&nbsp; Theme by <a href="http://octopress.org">Octopress</a></span>
</p>

</footer>

  
    




</body>
</html>